package com.pudu.mydemo.act;

import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.graphics.drawable.AnimationDrawable;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.pudu.mydemo.adapter.AbstractWheelTextAdapter;
import com.pudu.mydemo.fragment.WifiLoginBlurDialogFragment;
import com.pudu.mydemo.remote.RobotApi;
import com.pudu.mydemo.utils.FasterAnimationDrawable;

import com.pudu.mydemo.utils.MyAnimationDrawable;
import com.pudu.mydemo.utils.UpdateManager;
import com.pudu.pdrobot.App;
import com.pudutech.pdrobot.R;

import java.util.Calendar;

/**
 * Created by guiliu on 2016/7/15.
 */
public class SmailTest extends BaseHomeActivity {

    private ImageButton home;
    private ImageButton begin,pause;
    ImageView imageView;
    private int first, then ,after,goback,current;
    private Intent intent;
    private int status;

    private Button button1,button2,button3,button4,button5, button8,button9,button10,button11,button12;
    //    private MyAnimationDrawable fasterAnimationDrawable;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        fasterAnimationDrawable = FasterAnimationDrawable.getInstance();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_smail_test;
    }

    @Override
    public void initView() {
        home = (ImageButton)findViewById(R.id.home);
        begin = (ImageButton)findViewById(R.id.begin);
        pause = (ImageButton)findViewById(R.id.pause);
        button1 = (Button)findViewById(R.id.button);
        button2 = (Button)findViewById(R.id.button3);
        button3 = (Button)findViewById(R.id.button5);
        button4 = (Button)findViewById(R.id.button6);
        button5 = (Button)findViewById(R.id.button7);
        button8 = (Button)findViewById(R.id.button8);
        button9 = (Button)findViewById(R.id.button9);
        button10 = (Button)findViewById(R.id.button10);
        button11 = (Button)findViewById(R.id.button11);
        button12 = (Button)findViewById(R.id.button12);
    }


    @Override
    public void initEvent() {
        home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SmailTest.this, HomeActivity.class);
                startActivity(intent);
                SmailTest.this.finish();
            }
        });

        begin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(getApplicationContext(),"开始",Toast.LENGTH_LONG).show();
                begin.setVisibility(View.GONE);
                pause.setVisibility(View.VISIBLE);
//                changeAnimationOlder();
            }
        });

        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(getApplicationContext(),"暂停",Toast.LENGTH_LONG).show();
                begin.setVisibility(View.VISIBLE);
                pause.setVisibility(View.GONE);
            }
        });

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeAnimationOlder(1);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeAnimationOlder(2);
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeAnimationOlder(3);
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeAnimationOlder(4);
            }
        });
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeAnimationOlder(5);
            }
        });


        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SmailTest.this, VideoActivity.class);
                intent.putExtra("status",8);
                startActivity(intent);
            }
        });
        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SmailTest.this, VideoActivity.class);
                intent.putExtra("status",9);
                startActivity(intent);
            }
        });
        button10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SmailTest.this, VideoActivity.class);
                intent.putExtra("status",10);
                startActivity(intent);
            }
        });
        button11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SmailTest.this, VideoActivity.class);
                intent.putExtra("status",11);
                startActivity(intent);
            }
        });
        button12.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SmailTest.this, VideoActivity.class);
                intent.putExtra("status",12);
                startActivity(intent);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        intent = getIntent();
        status = intent.getIntExtra("status",0);
        imageView = (ImageView)findViewById(R.id.animation1);
        MyAnimationDrawable.setShouldRun(true);
        first = R.drawable.smail_animation;
//        first = R.drawable.smail_animation_new;
        //下面是一开始就没有在这个动画界面的过程
        setOlderByStatus(status);

//        then =  R.drawable.smail_left_animation;
//        after = R.drawable.smail_inleft_animation;
//        goback = R.drawable.smail_left__right_animation;


        //中间-》左边眨眼-》右边眨眼-》中间
//        then = R.drawable.smail_left_animation;
//        after = R.drawable.smail_left_right_back_animation;
//        goback = R.drawable.smail_right_center_animation;

        //中间-》发呆-》中间
//        then = R.drawable.daydreaming_start_animation;
//        after = R.drawable.daydreaming_go_animation;
//        goback = R.drawable.daydreaming_back_animation;

        //中间-》没电-》中间(存在问题)
//        then = R.drawable.no_electricity_start_animation;
//        after = R.drawable.no_electricity_go_animation;
//        goback = R.drawable.no_electricity_back_animation;
//        after = R.drawable.smail_inleft_animation;

        current = first;
        playAnimation(first, true);
        imageView.setOnClickListener(new NoDoubleOnclick(){

            @Override
            public void onNoDoubleClick(View v) {
//                MyAnimationDrawable.pause();
//                playAnimation(R.drawable.smail_touch_animation, false);
                if(current == first){
                    current = then;
                }else if(current == then) {
                    current = after;
                }else if(current == after) {
                    current = goback;
                }
                if(current != 0) {
                    playAnimation(current, false);
                } else {
                    current = first;
                    playAnimation(current, true);
                }
            }
        });

    }

    public void setOlderByStatus(int status) {
        if(status == 0) {
            //被触摸大笑
            then = R.drawable.smail_touch_animation;
        } else if(status == 1) {
            //中间-》左边笑-》中间
            then = R.drawable.smail_left_animation;
            after = R.drawable.smail_inleft_animation;
            goback = R.drawable.smail_left__back_animation;
        } else if(status == 2) {
            //中间-》右边笑-》中间
            then = R.drawable.smail_center__right_animation;
            after = R.drawable.smail_inright_animation;
            goback = R.drawable.smail_right_center_animation;
        } else if(status == 3) {
            //中间-》左边眨眼-》右边眨眼-》中间
            Log.e("guiliu","changeAnimationOlder");
            then = R.drawable.smail_left_animation;
            after = R.drawable.smail_left_right_back_animation;
            goback = R.drawable.smail_right_center_animation;
        } else if(status == 4) {
            //中间-》发呆-》中间
            then = R.drawable.daydreaming_start_animation;
            after = R.drawable.daydreaming_go_animation;
            goback = R.drawable.daydreaming_back_animation;

        } else if(status == 5){
            then = R.drawable.no_electricity_start_animation;
            after = R.drawable.no_electricity_go_animation;
            goback = R.drawable.no_electricity_back_animation;
//            after = R.drawable.smail_inleft_animation;
        }
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    public void changeAnimationOlder(int status) {
        setOlderByStatus(status);
        current = then;
        playAnimation(current,false);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MyAnimationDrawable.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyAnimationDrawable.stop();
    }

    public void playAnimation(int resId, final boolean oneShot) {
//        MyAnimationDrawable.pause();
        MyAnimationDrawable.setOneShot(oneShot);
        MyAnimationDrawable.animateRawManuallyFromXML(resId, imageView, new Runnable() {
            @Override
            public void run() {
                // TODO onStart
                // 动画开始时回调
            }
        }, new Runnable() {
            @Override
            public void run() {
                // TODO onComplete
                // 动画结束时回调
//                Log.e("guiliu","onComplete oneShot"+ oneShot);
                if(!oneShot)  {
                    if(current == R.drawable.smail_touch_animation) {
//                        Log.e("guiliu","onComplete oneShot"+ current);
                        current = first;
                        playAnimation(current, true);
                        return;
                    }
                    if(current == first){
                        current = then;
                    }else if(current == then) {
                        current = after;
                    }else if(current == after) {
                        current = goback;
                    }else if(current == goback) {
                        current = first;
                    }

                    if(current !=0) {
                        playAnimation(current, true);
                    } else {
                        current = first;
                        playAnimation(current, true);
                    }

                }
            }
        });
    }

    public abstract class NoDoubleOnclick implements View.OnClickListener{
        public static final int MIN_CLICK_DELAY_TIME = 1000;
        private long lastClickTime = 0;
        @Override
        public void onClick(View v) {
            long currentTime = Calendar.getInstance().getTimeInMillis();
            if(currentTime - lastClickTime > MIN_CLICK_DELAY_TIME ){
                lastClickTime = currentTime;
                onNoDoubleClick(v);
            }
        }

        public abstract void onNoDoubleClick(View v);
    }


   /* @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if(intent.getAction() != null && intent.getAction().equals("android.intent.battery.low")) {
            //电量不足
            Log.e("barrery","电量不足");
            Toast.makeText(this,"电量不足",Toast.LENGTH_LONG).show();
        }
    }*/


    /**
     * 通过粘性广播检测电量
     */
    private void checkBattery()
    {
        //通过粘性广播读取电量
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent intentBattery = registerReceiver(null, intentFilter);//注意，粘性广播不需要广播接收器
        if(intentBattery!=null)
        {
            //获取当前电量
            int batteryLevel = intentBattery.getIntExtra("level", 0);
            //电量的总刻度
            int batterySum = intentBattery.getIntExtra("scale", 100);
            float rotatio = 100*(float)batteryLevel/(float)batterySum;
            Log.d("currentBattery","currentBattery="+rotatio+"%");
            if(rotatio<38)
            {
                getWindow().getDecorView().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        showAlertToastTip("common_low_batter");
                    }
                }, 100);
            }
        }
    }
    /**
     * 显示警告提示
     * @param msg
     */
    private void showAlertToastTip(String msg)
    {
        Toast.makeText(this,"msg:"+msg,Toast.LENGTH_LONG).show();
    }

    //        first = R.drawable.smail_animation_new;
    /*    //下面是一开始就没有在这个动画界面的过程
        if(status == 0) {
            //被触摸大笑
            then = R.drawable.smail_touch_animation;
        } else if(status == 1) {
            //中间-》左边笑-》中间
        then = R.drawable.smail_left_animation;
        after = R.drawable.smail_inleft_animation;
        goback = R.drawable.smail_left__back_animation;
        } else if(status == 2) {
            //中间-》右边笑-》中间
        then = R.drawable.smail_center__right_animation;
        after = R.drawable.smail_inright_animation;
        goback = R.drawable.smail_right_center_animation;
        } else if(status == 3) {
            //中间-》左边眨眼-》右边眨眼-》中间
            Log.e("guiliu","changeAnimationOlder");
            then = R.drawable.smail_left_animation;
            after = R.drawable.smail_left_right_back_animation;
            goback = R.drawable.smail_right_center_animation;
        } else if(status == 4) {
            //中间-》发呆-》中间
        then = R.drawable.daydreaming_start_animation;
        after = R.drawable.daydreaming_go_animation;
        goback = R.drawable.daydreaming_back_animation;

        } else if(status == 5){
            then = R.drawable.no_electricity_start_animation;
        after = R.drawable.no_electricity_go_animation;
        goback = R.drawable.no_electricity_back_animation;
        after = R.drawable.smail_inleft_animation;
        }*/

//        then =  R.drawable.smail_left_animation;
//        after = R.drawable.smail_inleft_animation;
//        goback = R.drawable.smail_left__right_animation;


    //中间-》左边眨眼-》右边眨眼-》中间
//        then = R.drawable.smail_left_animation;
//        after = R.drawable.smail_left_right_back_animation;
//        goback = R.drawable.smail_right_center_animation;

    //中间-》发呆-》中间
//        then = R.drawable.daydreaming_start_animation;
//        after = R.drawable.daydreaming_go_animation;
//        goback = R.drawable.daydreaming_back_animation;

    //中间-》没电-》中间(存在问题)
//        then = R.drawable.no_electricity_start_animation;
//        after = R.drawable.no_electricity_go_animation;
//        goback = R.drawable.no_electricity_back_animation;
//        after = R.drawable.smail_inleft_animation;
}
